package cc.ufinity.networkdiagnose.manager;
 
import java.util.List;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener; 
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.widget.TextView;

public class LocManager{
    private LocationManager manager;
    private SharedPreferences sp;
    private TextView textView;
    private int num;
    public static String latitude = "0.0";
    public static String longitude = "0.0";
    public LocManager(Activity ctx){
    	 try {
    		 manager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);//获取手机位置信息
	          List<String> providers = manager.getAllProviders(); 
	          for (String provider : providers) {
	              System.out.println(provider);
	          }
	          
	         //获取的条件
	          Criteria criteria = new Criteria();
	          criteria.setAccuracy(Criteria.ACCURACY_FINE);//获取精准位置
	          criteria.setCostAllowed(true);//允许产生开销
	          criteria.setPowerRequirement(Criteria.POWER_HIGH);//消耗大的话，获取的频率高
	          criteria.setSpeedRequired(true);//手机位置移动
	          criteria.setAltitudeRequired(true);//海拔
	          
	          //获取最佳provider: 手机或者模拟器上均为gps
	          String bestProvider = manager.getBestProvider(criteria, true);//使用GPS卫星
	          Location location = null;
         
        	  if(bestProvider == null)
    	  	  {
                    ContentResolver cr = ctx.getContentResolver();
    	  			Secure.setLocationProviderEnabled(cr, LocationManager.GPS_PROVIDER, true);
    	  			Secure.setLocationProviderEnabled(cr, LocationManager.NETWORK_PROVIDER, true);
    	  			manager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);

     	  	  }
	          if(bestProvider != null){
	        	  location = manager.getLastKnownLocation(bestProvider);
	          	  if(location != null){
	                  latitude = location.getLatitude()+"";//经度
	                  longitude = location.getLongitude()+"";//纬度
	               }
	              sp = ctx.getSharedPreferences("config",Context.MODE_PRIVATE);
	              //parameter: 1. provider 2. 每隔多少时间获取一次  3.每隔多少米  4.监听器触发回调函数
	              manager.requestLocationUpdates(bestProvider,3000,8, new MyLocationListener());
	          }
		  } catch (Exception e) {
			// TODO: handle exception
		  }
    }
    
    
    private class MyLocationListener implements LocationListener{
        
        public void onLocationChanged(Location location) {
        	if(location != null){
        		location.getAccuracy();//精确度
                latitude = location.getLatitude()+"";//经度
                longitude = location.getLongitude()+"";//纬度
              //  updateView(location,"位置变化"+num+manager.isProviderEnabled(LocationManager.GPS_PROVIDER));
                //将获取到的经纬度信息存入SharedPreferences
                Editor editor = sp.edit();
                editor.putString("lastloaction", latitude + "-" + longitude);
                editor.commit();
                num++;
        	}
        	else {
        		//updateView(location,"没有数据"+num);
			}
            
        }
        public void onStatusChanged(String provider, int status, Bundle extras) {
            
        }
         
        public void onProviderEnabled(String provider) {
            
        }
 
        public void onProviderDisabled(String provider) {
            
        }
    }
 // 更新TextView中显示的内容
 	public void updateView(Location newLocation,String flag)
 	{
 		System.err.println(flag);
 		if (newLocation != null)
 		{
 			StringBuilder sb = new StringBuilder();
 			sb.append("标记：");
 			sb.append(flag);
 			sb.append("\n精度：");
 			sb.append(newLocation.getAccuracy());
 			sb.append("\n实时的位置信息：\n");
 			sb.append("经度：");
 			sb.append(newLocation.getLongitude());
 			sb.append("\n纬度：");
 			sb.append(newLocation.getLatitude());
 			sb.append("\n高度：");
 			sb.append(newLocation.getAltitude());
 			sb.append("\n速度：");
 			sb.append(newLocation.getSpeed());
 			sb.append("\n方向：");
 			sb.append(newLocation.getBearing()); 
 
 		}
 		 
 	}
}

